package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds,Integer[] menuIds){
        try {
            roleService.add(role, permissionIds,menuIds);
        }catch (Exception e){
            //新增失败
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);//新增角色失败
        }
        //新增成功
        return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);//新增角色成功
    }

    //分页查询
    @RequestMapping("/pageQuery")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = roleService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    //删除
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('ROLE_DELETE')")//权限校验
    public Result delete(Integer id){
        try {
            roleService.delete(id);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }catch (Exception e){
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
    }

    //编辑
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")//权限校验
    public Result edit(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        try {
            roleService.edit(role,permissionIds,menuIds);

        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
    }

    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findById(Integer id){
        try{
            Role role = roleService.findById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,role);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }


    //根据检查组合id查询对应的所有检查项id
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id){
        try{
            List<Integer> PermissionIds = roleService.findPermissionIdsByRoleId(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, PermissionIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }


    //根据检查组合id查询对应的所有检查项id
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id){
        try{
            List<Integer> menu = roleService.findMenuIdsByRoleId(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, menu);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //查询所有
    @RequestMapping("/findPermissionAll")
    public Result findPermissionAll(){
        List<Permission> permissionList = roleService.findPermissionAll();
        if(permissionList != null && permissionList.size() > 0){
            Result result = new Result(true, MessageConstant.GET_PERMISSIONS_INFORMATION_SUCCESS);//获取权限信息成功
            result.setData(permissionList);
            return result;
        }
        return new Result(false, MessageConstant.GET_PERMISSIONS_INFORMATION_FAIL);//获取权限信息失败
    }

    //查询所有
    @RequestMapping("/findMenuAll")
    public Result findMenuAll(){
        List<Menu> menuList = roleService.findMenuAll();
        if(menuList != null && menuList.size() > 0){
            Result result = new Result(true, MessageConstant.GET_MRNU_SUCCESS);//获取权限信息成功
            result.setData(menuList);
            return result;
        }
        return new Result(false, MessageConstant.GET_MENU_FAIL);//获取权限信息失败
    }

    @RequestMapping("/findAll")
    public Result findAll(){

        List<Role> roleList = roleService.findAll();
        if(roleList != null && roleList.size() > 0){
            Result result = new Result(true,
                    MessageConstant.QUERY_CHECKITEM_SUCCESS);
            result.setData(roleList);
            return result;
        }
        return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
    }
}